using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Ricciolo.PaperBoy.UI.Interop;

namespace Ricciolo.PaperBoy.UI
{
    [ComVisible(true)]
    public class WebBrowserController : IOleClientSite
    {

        private WebBrowser webBrowser;

        private const int DISPID_AMBIENT_DLCONTROL = -5512;
        private bool clientSiteSet = false;

        public WebBrowserController(WebBrowser webBrowser)
        {
            this.webBrowser = webBrowser;
            this.webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser_Navigating);
        }

        void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            // Only first time
            if (!clientSiteSet)
            {
                this.SetClientSite();
                clientSiteSet = true;
            }
        }

        public void SetClientSite()
        {
            ((IOleObject)webBrowser.ActiveXInstance).SetClientSite(this);
            ((IOleControl)webBrowser.ActiveXInstance).OnAmbientPropertyChange(DISPID_AMBIENT_DLCONTROL);
        }

        [DispId(DISPID_AMBIENT_DLCONTROL)]
        public int setFlags()
        {
            Debug.WriteLine("setFlags");
            //return 0;
            return (int)(DLCTL.SILENT | DLCTL.FORCEOFFLINE | DLCTL.NO_BEHAVIORS | DLCTL.NO_CLIENTPULL | DLCTL.NO_DLACTIVEXCTLS | DLCTL.NO_FRAMEDOWNLOAD | DLCTL.NO_JAVA | DLCTL.NO_RUNACTIVEXCTLS | DLCTL.NO_SCRIPTS);
        }

        #region IOleClientSite

        int IOleClientSite.SaveObject()
        {
            return 0;
        }

        int IOleClientSite.GetMoniker(int dwAssign, int dwWhichMoniker, out object moniker)
        {
            moniker = null;
            return 0;
        }

        int IOleClientSite.GetContainer(out IOleContainer container)
        {
            container = null;
            return 0;
        }

        int IOleClientSite.ShowObject()
        {
            return 0;
        }

        int IOleClientSite.OnShowWindow(int fShow)
        {
            return 0;
        }

        int IOleClientSite.RequestNewObjectLayout()
        {
            return 0;
        }

        #endregion
    }
}
